import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yi_shop/model/member.dart';
import 'package:yi_shop/ui/draw_under_line.dart';
import 'package:yi_shop/ui/scroll_refresh_config.dart';
import 'package:yi_shop/utils/colors.dart';
import 'package:yi_shop/utils/styles.dart';
import 'package:yi_shop/utils/tools.dart';

class MemberCenterGrowController extends GetxController {
  RxList<MemberGrowModel> pageList = RxList([]);

  RefreshController refreshController = RefreshController();

  @override
  void onInit() {
    pageList.add(MemberGrowModel(
        title: '完成每日登录',
        loginTime: DateFormat('yyyy-MM-dd HH:mm').format(DateTime.now()),
        grow: 10));
    initData();
    super.onInit();
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 32) {
      return refreshController.loadNoData();
    }

    /// 添加数据
    initData();

    /// 完毕
    refreshController.loadComplete();
  }

  void initData() {
    var r = Random.secure();
    for (var i = 0; i < 10; i++) {
      pageList.add(MemberGrowModel(
          title: '完成每日登录',
          loginTime:
              DateFormat('yyyy-MM-dd HH:mm').format(DateTime.now().subtract(Duration(days: i + 1))),
          grow: r.nextInt(20)));
    }
  }
}

class MemberCenterGrowPage extends GetView<MemberCenterGrowController> {
  const MemberCenterGrowPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: ToolsUtils.builderLeading(),
        title: Text(
          '成长值记录',
          style: StylesUtils.customTextStyle(
              fontWeight: FontWeight.w700, fontSize: StylesUtils.fontSize18),
        ),
      ),
      body: Obx(() => scrollRefreshConfig(context)),
    );
  }

  ScrollRefreshConfig scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: listViewBuilder(context),
    );
  }

  ListView listViewBuilder(BuildContext context) {
    return ListView.builder(
      itemBuilder: (_, int index) {
        var row = controller.pageList[index];
        return DrawUnderLine(
          color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF2f2),
          child: Container(
            height: 70,
            padding: const EdgeInsets.only(left: 15, right: 25),
            decoration: const BoxDecoration(color: Colors.white),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(
                    child: Column(
                  children: [
                    Container(
                      padding: const EdgeInsets.only(top: 10),
                      alignment: Alignment.centerLeft,
                      child: Text(
                        row.title,
                        style: StylesUtils.customTextStyle(
                            fontWeight: FontWeight.w500, color: ColorsUtils.color3333),
                      ),
                    ),
                    Container(
                      padding: const EdgeInsets.only(top: 15),
                      alignment: Alignment.centerLeft,
                      child: Text(
                        row.loginTime,
                        style: StylesUtils.customTextStyle(color: ColorsUtils.color9999),
                      ),
                    )
                  ],
                )),
                Center(
                  child: Text(
                    '+${row.grow}',
                    style: StylesUtils.customTextStyle(color: ColorsUtils.colorF048),
                  ),
                )
              ],
            ),
          ),
        );
      },
      itemCount: controller.pageList.length,
      itemExtent: 70,
    );
  }
}
